// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DBContainerHelpers.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.DataModel
{
    #region Usings

    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Org.OpenTrader.Framework.LiveObjects.Interfaces;

    #endregion

    /// <summary>
    /// The db container helpers.
    /// </summary>
    public class DBContainerHelpers
    {
        #region Public Methods

        /// <summary>
        /// The destroy.
        /// </summary>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <returns>
        /// The destroy.
        /// </returns>
        public static bool Destroy(Guid id)
        {
            var dbContainers = from r in Singleton<DBFramework>.Instance.Entities.DBContainer where r.ID == id select r;

            if (dbContainers.Count() != 1)
            {
                return false;
            }

            // Delete the container (cascade delete the container resources)
            Singleton<DBFramework>.Instance.Entities.DeleteObject(dbContainers.First());

            return false;
        }

        /// <summary>
        /// The get container.
        /// </summary>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <returns>
        /// </returns>
        public static DBContainer GetContainer(Guid id)
        {
            var query = from r in Singleton<DBFramework>.Instance.Entities.DBContainer where r.ID == id select r;

            if (query.Count() > 0)
            {
                return query.First();
            }

            return null;
        }

        /// <summary>
        /// The get resources.
        /// </summary>
        /// <param name="containerId">
        /// The containerId.
        /// </param>
        /// <returns>
        /// </returns>
        public static IResource[] GetResources(Guid containerId)
        {
            var dbContainers = from r in Singleton<DBFramework>.Instance.Entities.DBContainer
                               where r.ID == containerId
                               select r;

            var dbContainer = dbContainers.First();

            dbContainer.DBContainerResource.Load();
            IList<IResource> resources = new List<IResource>();
            foreach (var dbContainerResource in dbContainer.DBContainerResource)
            {
                dbContainerResource.DBResourceReference.Load();
                var r = DBResourceHelpers.Create(dbContainerResource.DBResource);
                resources.Add(r);
            }

            return resources.ToArray();
        }

        #endregion
    }
}